﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Core;

namespace Ue.Tests.Core
{
    /// <summary>
    /// LazyField 的摘要说明
    /// </summary>
    [TestClass]
    public class LazyFieldTests
    {
        [TestMethod]
        public void LazyField()
        {
            LazyField<int> lazyInt = new LazyField<int>(() => { return 5; });

            Assert.AreEqual(5, lazyInt.Value);
        }

        [TestMethod]
        public void LoadedOnlyOnce()
        {
            object obj = new object();
            object obj2 = new object();
            Assert.IsFalse(obj.Equals(obj2));

            LazyField<object> lazyObj = new LazyField<object>(() => { return new object(); });
            Assert.IsTrue(lazyObj.Value.Equals(lazyObj.Value));
        }

        [TestMethod]
        public void ChangeTheLoaderWillReload()
        {
            LazyField<int> lazyInt = new LazyField<int>(() => { return 5; });
            Assert.AreEqual(5, lazyInt.Value);

            lazyInt.Loader(() => { return 10; });
            Assert.AreEqual(10, lazyInt.Value);
        }

        [TestMethod]
        public void LoaderIsNullThenValueIsDefault()
        {
            LazyField<int> lazyInt = new LazyField<int>();

            Assert.AreEqual(new int(), lazyInt.Value);
        }

        [TestMethod]
        public void FieldSetter()
        {
            LazyField<int> lazyInt = new LazyField<int>(number => { return number + 1; });

            int mockNumber = 1;
            lazyInt.Value = mockNumber;
            Assert.AreEqual(mockNumber + 1, lazyInt.Value);

            mockNumber = 2;
            lazyInt.Value = mockNumber;
            Assert.AreEqual(mockNumber + 1, lazyInt.Value);
        }

        [TestMethod]
        public void ChangeTheSetter()
        {
            LazyField<int> lazyInt = new LazyField<int>(number => { return number + 1; });

            int mockNumber = 1;
            lazyInt.Value = mockNumber;
            Assert.AreEqual(mockNumber + 1, lazyInt.Value);

            mockNumber = 2;
            lazyInt.Setter(number => { return number + 5; });
            lazyInt.Value = mockNumber;
            Assert.AreEqual(mockNumber + 5, lazyInt.Value);
        }
    }
}
